package com.twu.biblioteca.menu;

import com.twu.biblioteca.*;

public abstract class CheckInOutMenuItem implements MenuItem {
    private LibraryItemFinder itemFinder;

    public CheckInOutMenuItem(LibraryItemFinder itemFinder) {
        this.itemFinder = itemFinder;
    }

    @Override
    public PostMenuItemAction execute(Library library, UserIO io, UserRepository repository) {
        if (repository.getLoggedInUser() == null) {
            io.println("You must log in before you can do that.");
            return PostMenuItemAction.Continue;
        }

        io.print(getPromptMessage());
        LibraryItem selectedItem = getItemFinder().findLibraryItem(io.readLine(), library);

        if (selectedItem != null && performOperation(selectedItem, library, repository.getLoggedInUser())) {
            io.println(getSuccessMessage());
        } else {
            io.println(getFailureMessage());
        }

        return PostMenuItemAction.Continue;
    }

    @Override
    public boolean shouldDisplay(User currentUser) {
        return true;
    }

    protected abstract String getPromptMessage();
    protected abstract String getSuccessMessage();
    protected abstract String getFailureMessage();
    protected abstract boolean performOperation(LibraryItem selectedItem, Library library, User currentUser);

    public LibraryItemFinder getItemFinder() {
        return itemFinder;
    }

    public void setItemFinder(LibraryItemFinder itemFinder) {
        this.itemFinder = itemFinder;
    }
}
